$(function () {
    $("#file_upload_1").uploadify({
        height: 30,
        buttonImage: "",
        swf: 'uploadify/uploadify.swf',
        uploader: 'uploadTitleImage',
        width: 120,
        fileObjName: 'uploadfile',
        progressData: 'speed',
        multi: false,
        preventCaching: false,
        auto: false,
        fileTypeDesc: '不超过2M的图片 (*.gif;*.jpg;*.png)',
        fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png',
        cancelImg: 'uploadify/uploadify-cancel.png',//取消图片路径
        fileSizeLimit: 2048,  //允许上传的文件大小(kb)  此为2M
        uploadLimit: 999,
        buttonText: '浏览',
        onUploadError: function (file, errorCode, errorMsg, errorString) {
            alert('图片 ' + file.name + ' 上传失败: ' + errorString);
        },
        onUploadSuccess: function (file, data, response) {
            document.getElementById("titleImg").setAttribute("src", data);
            document.getElementById("preview48").setAttribute("src", data);
            document.getElementById("preview96").setAttribute("src", data);
            document.getElementById("oldpath").setAttribute("value", data);
            document.getElementById("imgtable").style.display = "";
            jQuery(function ($) {
                // Create variables (in this scope) to hold the API and image size
                var jcrop_api, boundx, boundy;

                $("#titleImg").Jcrop({
                    onChange: updatePreview,
                    onSelect: updatePreview,
                    aspectRatio: 1
                }, function () {
                    // Use the API to get the real image size
                    var bounds = this.getBounds();
                    boundx = bounds[0];
                    boundy = bounds[1];
                    // Store the API in the jcrop_api variable
                    jcrop_api = this;
                    jcrop_api.animateTo([100, 100, 400, 300]);
                });

                function updatePreview(c) {
                    if (parseInt(c.w) > 0) {
                        var rx48 = 48 / c.w;
                        var ry48 = 48 / c.h;
                        var rx96 = 96 / c.w;
                        var ry96 = 96 / c.h;

                        $("#preview48").css({
                            width: Math.round(rx48 * boundx) + "px",
                            height: Math.round(ry48 * boundy) + "px",
                            marginLeft: "-" + Math.round(rx48 * c.x) + "px",
                            marginTop: "-" + Math.round(ry48 * c.y) + "px"
                        });
                        $("#preview96").css({
                            width: Math.round(rx96 * boundx) + "px",
                            height: Math.round(ry96 * boundy) + "px",
                            marginLeft: "-" + Math.round(rx96 * c.x) + "px",
                            marginTop: "-" + Math.round(ry96 * c.y) + "px"
                        });
                        $("#x1").val(c.x);
                        $("#y1").val(c.y);
                        $("#w").val(c.w);
                        $("#h").val(c.h);
                    }
                    ;
                };
            });

        }
    });
    });


function thumbnailimage() {
    $.ajax({
        url: "/Origami/thumbnailimage",
        type: "post",
        data: {x1: $("#x1").val(), y1: $("#y1").val(), w: $("#w").val(), h: $("#h").val(), oldpath: $("#oldpath").val()},
        dataType: "json",
        complete: function (data) {
            if(data.responseText == "error"){
                $("#savemsg").html("系统错误保存失败");
            }else{
                $("#savemsg").html("图片保存成功")
                $("#saveimgmsg").html("");
                $("#imgsrc").val(data.responseText);
            }
        }
    })
}
function isURL(str_url) {// 验证url
    var strRegex = "[a-zA-z]+://[^\s]*";
    var re = new RegExp(strRegex);
    return re.test(str_url);
}

var ue = UE.getEditor('uecontainer');
function check(title, introduction, imgsrc,
videourl, category) {

    if (title == null || title.trim() == "") {
        $("#titlemsg").html("标题不能为空");
        $("#title").focus();
        return false;
    } else if (title.length < 5 || title.length > 20) {
        $("#titlemsg").html("标题长度为5-20位");
        $("#title").focus();
        return false;
    } else {
        $("#titlemsg").html("");
    }
    if (introduction == null || introduction.trim() == "") {
        $("#introductionmsg").html("简介不能为空");
        $("#introduction").focus();
        return false;
    } else if (introduction.length > 200) {
        $("#introductionmsg").html("简介长度最大200");
        $("#introduction").focus();
        return false;
    } else {
        $("#introductionmsg").html("");
    }

    if (category == null || category.trim() == "") {
        $("#categorymsg").html("请选择类别");
        $("#category").focus();
        return false;
    }  else {
        $("#categorymsg").html("");
    }

    /*if(videourl != null || videourl.trim() != ""){
        if(!isURL(videourl)){
            $("#videomsg").html("请输入合法的视频地址");
            return false;
        }else{
            $("#videomsg").html("");
            $("#video_url").val(videourl);
        }
    }*/

    if (imgsrc == null || imgsrc.trim() == "") {
        $("#saveimgmsg").html("请上传标题图片");
        return false;
    } else {
        $("#saveimgmsg").html("");
    }
    if (!ue.hasContents()) {
        $("#contentmsg").html("请填写内容");
        return false;
    } else {
        $("#contentmsg").html("");
    }
    return true;
}

function gosubmit() {
    var title = $("#title").val();
    var introduction = $("#introduction").val();
    var imgsrc = $("#imgsrc").val();
    var videourl= $("#videourl").val();
    var category = $("#category").val();
    if (check(title,introduction,imgsrc,videourl,category)) {
        $("#titlecontext").val(title);
        $("#introductioncontext").val(introduction);
        $("#type").val(category);
        tutorialform.submit();
    }
}


function update(tutorialId) {
    var title = $("#title").val();
    var introduction = $("#introduction").val();
    var imgsrc = $("#imgsrc").val();
    var videourl = $("#videourl").val();
    var category = $("#category").val();
    if (check(title, introduction, imgsrc, videourl, category)) {
        $("#titlecontext").val(title);
        $("#introductioncontext").val(introduction);
        $("#type").val(category);
        updatetutorialform.submit();
    }
}
